package com.dj.ms.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.ObjectUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <br>
 * <p></p>
 *
 * <br>
 *
 * @author ZJG
 * @version 1.0
 * @date 2021/8/4 下午2:01
 */
public class ObjectUtil {

    private ObjectUtil(){}

    public static <T> T defaultIfEmpty(T object,T defaultObject){
        if(null == object){
            return defaultObject;
        }
        return object;
    }

    public static <T> T decode(T object,T ...params){
        int offset = params.length%2;
        for(int i=0;i<params.length-offset;i++){
            if(i%2==0){
                if(ObjectUtils.equals(object,params[i])){
                    return params[i+1];
                }
            }
        }
        return offset == 1?params[params.length-1]:object;
    }

    public static <T,E> T decodes(E object,Object ...params) throws Exception{
        int offset = params.length%2;
        for(int i=0;i<params.length-offset;i++){
            if(i%2==0){
                if(ObjectUtils.equals(object,params[i])){
                    return (T)params[i+1];
                }
            }
        }
        if(offset == 1){
            Object o  = params[params.length-1];
            if(o instanceof Exception){
                throw (Exception) o;
            }
            return (T)o;
        }
        return null;
    }

    /**
     *
     * @param params
     * @return
     */
    public static Map toMap(Object... params) {
        Map map = new HashMap();
        if (params.length % 2 == 0) {
            for (int i = 0; i < params.length; i = i + 2) {
                map.put(params[i], params[i + 1]);
            }
        }
        return map;
    }

    private static String getContainKey(String content, String keyField) {
        String patternStr = "\\[\\{.*";
        String pattern = "(\\w*)"+"\":"+patternStr + keyField;
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(content);
        String aimKey = "";
        if (m.find()) {
            aimKey = m.group(1);
        }
        return aimKey;
    }

    public static String reMsg(String message) {
        if (StrUtil.containsAny(message, "[")) {
            message = message.substring(message.lastIndexOf("["), message.lastIndexOf("]"))
                    .replaceAll("\\[", "")
                    .replaceAll("]", "");
            if (JSONUtil.isJson(message)) {
                JSONObject obj = JSONObject.parseObject(message);
                if (cn.hutool.core.util.ObjectUtil.isNotNull(obj) && obj.containsKey("msg")) {
                    return obj.getString("msg");
                }
            }
        }
        return StrUtil.containsAny(message, "FeignException", "InternalServerError")? "API接口异常": message;
    }

    public static int reCode(String message) {
        if (StrUtil.containsAny(message, "[")) {
            message = message.substring(message.lastIndexOf("["), message.lastIndexOf("]"))
                    .replaceAll("\\[", "")
                    .replaceAll("]", "");
            if (JSONUtil.isJson(message)) {
                JSONObject obj = JSONObject.parseObject(message);
                if (cn.hutool.core.util.ObjectUtil.isNotNull(obj) && obj.containsKey("code")) {
                    return obj.getInteger("code");
                }
            }
        }
        return 500;
    }
}
